<?php
class Quantity extends Controller
{
	private $tbl = 'tbl_publication_quantity';
 	function Quantity()
	{
		parent::Controller();
                $this->load->model('quantity_model');
                $this->load->helper('text');
	}
	
	function index()
	{
		$data['total_quantity'] = $this->quantity_model->total_quantity();
                create_pagination($data['total_quantity'], 3, config_item('per_page'));
                $data['quantities'] = $this->quantity_model->get_quantity(config_item('per_page'), $this->uri->segment(3));
		$data['page_title']	= 'Manage Publication Quantity';
		$data['page'] = 'quantity_list_v';
		$this->load->view('main', $data);	
	}
	
	function add()
	{
		if($this->input->post('submit_location'))
		{
			$this->add_edit();	
		}
		$data['page_title']	 = 'Add New Locations';
		$data['page'] = 'locations_add_v';
		$this->load->view('main', $data);
	}
	
	function edit($publication_id)
	{
		if($this->input->post('submit_quantity'))
		{
			$this->add_edit($publication_id);
		}
		//$data['categories'] = $this->general_db_model->getAll($this->tbl, 'locations_name', 'parent_id = 0');
		$data['_quantity'] = $this->general_db_model->getById($this->tbl, 'publication_id', $publication_id);
		$data['page_title']	 = 'Update Publication Quantity';
		$data['page'] = 'quantity_add_v';
		$this->load->view('main', $data);	
	}
	
	function add_edit($id = NULL)
	{
		 $data['location_name'] = $this->input->post('location_name');
		 if($id)
		 {
		 	$this->general_db_model->update($this->tbl, $data, 'id = '.$id);
			$this->session->set_flashdata('success_message', "Location - <strong>".$data['location_name']." </strong>Successfully updated.");
		 }
		 else
		 {
		 	$this->general_db_model->insert($this->tbl, $data);
		 	$this->session->set_flashdata('success_message', "Location - <strong>".$data['location_name']." </strong>Successfully added.");
		 }
		 
		 redirect(current_url());
	}
	
	function delete()
	{
	 	$location_id = $this->input->post('c_id');
		$this->general_db_model->delete($this->tbl, 'id = '.$location_id);
	}
        function ajax_update_quantity(){
            $publication_id = $this->input->post('publication_id');
            $total_qty_distribution_en = $this->input->post("total_qty_distribution_en_$publication_id");
            $total_qty_distribution_np = $this->input->post("total_qty_distribution_np_$publication_id");
            $total_qty_box_en = $this->input->post("total_qty_box_en_$publication_id");
            $total_qty_box_np = $this->input->post("total_qty_box_np_$publication_id");
            $total_qty_store_en = $this->input->post("total_qty_store_en_$publication_id");
            $total_qty_store_np = $this->input->post("total_qty_store_np_$publication_id");
            $total_qty_grand_en = $this->input->post("total_qty_grand_en_$publication_id");
            $total_qty_grand_np = $this->input->post("total_qty_grand_np_$publication_id");
            $data_en['total_qty'] = $total_qty_grand_en;
            $data_en['total_qty_distribution'] = $total_qty_distribution_en;
            $data_en['total_qty_box'] = $total_qty_box_en;
            $data_en['total_qty_store'] = $total_qty_store_en;
            
            $data_np['total_qty'] = $total_qty_grand_np;
            $data_np['total_qty_distribution'] = $total_qty_distribution_np;
            $data_np['total_qty_box'] = $total_qty_box_np;
            $data_np['total_qty_store'] = $total_qty_store_np;
            $this->general_db_model->update($this->tbl, $data_en, "publication_id = '$publication_id' and type='en'");
            $this->general_db_model->update($this->tbl, $data_np, "publication_id = '$publication_id' and type='np'");
            echo "<div class='success_message'>Publication - $publication_id: Successfully updated.</div>";
        }
}

?>